﻿///<reference path="../../scripts/jquery-1.4.4.min.js" />
///<reference path="../../scripts/jquery-ui.min.js" />
///<reference path="../../scripts/kooboo/kooboo.form.js" />


$(function () {



    function renderQuestions() {
        var isEven = false;
        $("#question-container").empty();
        for (var i = 0; i < questions.length; i++) {
            var item = questions[i];

            var row = $("#question-row").find(".template").clone();
            row.find("td:eq(0)").html(item.Question);
            row.find("td:eq(1)").html(item.QuestionBy);

            var productUrl = row.find("td:eq(2)").find("a");
            productUrl.html(item.ProductName);
            productUrl.attr("href", item.ProductUrl);

            row.find("td:eq(3)").html(item.QuestionDate);

            var fields = row.find("td:eq(4)");

            fields.find("a").attr("data-index", i);





            row.find("td:last").find("input").val(item.Id);
            row.find("td:last").find("form").attr("data-index", i);

            if (isEven) {
                row.addClass("even");
            }
            isEven = !isEven;

            if (item.AnswerDate.length < 1) {
                row.addClass("todo")
                row.find(".display-view").hide();
            } else {
                row.find(".edit-view").hide();
            }


            $("#question-container").append(row);

        }
    }


    //events
    //1. render quetions
    renderQuestions();

    //2. show answer
    $(".view").live("click", function (e) {

        var index = parseInt($(this).attr("data-index"));
        var item = questions[index];

        $("#view-dialog").find(".by").html(item.AnsweredBy);
        $("#view-dialog").find(".date").html(item.AnsweredDate);
        $("#view-dialog").find(".body").val(item.Answer);



        return false;
    });

    //3. show answer form
    $(".answer").live("click", function (e) {

        var index = parseInt($(this).attr("data-index"));
        var item = questions[index];

        $("#QuestionId").val(item.Id);
        $("#answer-form").find("[name=Answer]").val("");

        $("#answer-form").attr("data-index", index);


        $("#answer-form").find("textarea:first").focus();

        return false;
    });

    //4. answer
    $("#answer-form").submit(function () {
        var index = parseInt($(this).attr("data-index"));

        $(this).ajaxPost({
            success: function (data) {
                var item = questions[index];
                item.AnswerDate = data.AnswerDate;
                item.AnswerBy = data.AnswerBy;
                item.Answer = data.Answer;

                renderQuestions();

                $("#answer-dialog").dialog("close");
            },
            box: $("#answer_messagebox")
        });

        return false;
    });


    //5.delete
    $(".delete-form").live("submit", function () {

        var form = $(this);

        form.ajaxPost({
            success: function () {
                var index = parseInt(form.attr("data-index"));
                questions.splice(index, 1);
                renderQuestions();
            }
        });
        return false;
    });

    $(".delete-link").live("click", function (e) {
        e.preventDefault();
        if (confirm($(this).attr("message"))) {
            $(this).parents("form:first").submit();
        }
        return false;
    });
});
